<?php 
class Exam_Model_DbTable_Tests extends Engine_Db_Table{
    protected $_rowClass = 'Exam_Model_Test';
    
    public function userHadTest($user_id,$exam_id){
        $select = $this->select()
                       ->where('exam_id = ?',$exam_id)
                       ->where('owner_id = ?',$user_id) ;
                       
        $rows = $this->fetchRow($select);
        if($rows) return true;
        return false;
    }
    
    public function newTest($user_id,$exam,$answers,$options){
        $result = array('error'=>false,'message'=>'');
        if(is_numeric($exam)){
            $exam = Engine_Api::_()->getItem('exam_exams','exam');
        }
        if(!$exam){
            $result['error'] = true;
            $result['message'] = 'Exam you select not found.';
        }
        
        $answerTable = Engine_Api::_()->getDbtable('answers','exam');
        $trueAnswer = $answerTable->getTrueAnswer(array('exam_id'=>$exam->exam_id));
        if(empty($trueAnswer)){
            $result['error'] = true;
            $result['message'] = 'Can not answer of question.';
        }
        
        if($result['error'] == true) return $result;
        
        $db = $this->getAdapter();
        $db->beginTransaction();
        try{
            $test = $this->createRow();
            $test->exam_id = $exam->exam_id;
            $test->start_time = $options['starttime'];
            $test->end_time = $options['endtime'];
            $test->owner_id = $user_id;
            $t = $this->getScore($answers,$trueAnswer,$exam->getIdentity());
            $test->total_score = $t['score'];
            $test->total_correct = $t['total_correct'];
            $test->save();
            
            $totalscore = $exam->average_score * $exam->test_count +$test->total_score;
            $exam->test_count++;
            $exam->average_score = $totalscore/ $exam->test_count;
            $exam->save();
            
            $useranswerTable = Engine_Api::_()->getDbtable('useranswers','exam');
            $useranswerTable->insertAnswer($answers,$user_id,$exam->exam_id,$test->test_id);
            
            $db->commit();
        }
        catch(Exception $e){
            $db->rollBack();
            throw $e;
        }
         
        
    }
    
    protected function getScore($answers,$trueanswers = array(),$exam_id = 0){
        $score = 0;
        $total_correct = 0;
        $result = true;;
        if(empty($trueanswers)){
            $answerTable = Engine_Api::_()->getDbtable('answers','exam');
            $trueAnswers = $answerTable->getTrueAnswer(array('exam_id'=>$exam_id));
        }
        foreach($answers as $question_id => $answer){
            if(count($answer) == count($trueanswers[$question_id]['correct'])){
                $result = true;
                foreach($answer as $answer_id){
                    if(!in_array($answer_id,$trueanswers[$question_id]['correct'])) {
                        $result = false;
                        break;
                    };
                }
                if($result == true){
                    $score += $trueanswers[$question_id]['score'];
                    $total_correct++;
                }
            }
        }
        return array('score'=>$score,'total_correct'=>$total_correct);
    }
    
    public function getTestSelectBuyExam($exam_id,$user_id){
        $sl = $this->select()
                   ->where('exam_id = ?',$exam_id)
                   ->where('owner_id = ?',$user_id);
                   
        return $sl;
    }
    
    
    
    
}
    
?>